package com.itsu.spbspider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsu.spbspider.entity.Api;
import com.itsu.spbspider.entity.Job;
import com.itsu.spbspider.entity.News;
import com.itsu.spbspider.exception.ApiException;
import com.itsu.spbspider.exception.SystemException;
import com.itsu.spbspider.mapper.ApiMapper;
import com.itsu.spbspider.mapper.JobMapper;
import com.itsu.spbspider.mapper.NewsMapper;
import com.itsu.spbspider.service.NewsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author suben
 * @create time 2020/2/6 15:30
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsMapper newsMapper;

    @Resource
    private JobMapper jobMapper;

    @Resource
    private ApiMapper apiMapper;

    @Override
    public List<News> getAllNews() throws SystemException {
        try {
            return newsMapper.selectList(null);
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }


    @Override
    public IPage<News> getByPage(Integer current, Integer pageSize) throws SystemException {
        try {
            Page<News> page = new Page<>();
            page.setCurrent(current).setSize(pageSize);
            QueryWrapper<News> condition = new QueryWrapper<>();
            condition.orderByDesc("ts");
            return newsMapper.selectPage(page, condition);
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    @Override
    public IPage<News> getByJobNamePage(Integer current, Integer pageSize, String jobName) throws SystemException {
        try {
            QueryWrapper<Job> condition = new QueryWrapper<>();
            condition.select("id").eq("job_name", jobName);
            Job job = jobMapper.selectOne(condition);

            Page<News> page = new Page<>();
            page.setCurrent(current).setSize(pageSize);
            QueryWrapper<News> con2 = new QueryWrapper<>();
            con2.eq("job_id", job.getId());
            return newsMapper.selectPage(page, con2);
        } catch (Exception e) {
            throw new SystemException(e);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<News> randomGetNews(String appKey, Integer size) throws Exception {
        Api api = null;
        try {
            api = apiMapper.selectById(appKey);
            if (api == null) {
                throw new ApiException(101, "appKey {" + appKey + "} 不存在");
            }
            if (api.getRemainingTimes() <= 0) {
                throw new ApiException(102, "appKey {" + appKey + "} 剩余次数不足");
            }
        } catch (ApiException e) {
            throw e;
        }
        try {

            Integer total = newsMapper.selectCount(null);
            if (size.intValue() > total) {
                throw new SystemException("请求数据size > 数据总数");
            }
            int index = new Random().nextInt(total / size);
            Page<News> page = new Page<>();
            page.setSize(size);
            page.setCurrent(index);
            List<News> newsList = newsMapper.selectPage(page, null).getRecords();
            api.setRemainingTimes(api.getRemainingTimes() - 1);
            apiMapper.updateById(api);
            return newsList;
        } catch (Exception e) {
            throw new SystemException(e);
        }

    }

}
